package com.asiainfo.cms.common.security.userdetails;


import cn.hutool.core.util.ObjectUtil;
import com.asiainfo.cms.common.constant.UserStatus;
import com.asiainfo.cms.common.exception.BizException;
import com.asiainfo.cms.common.service.SysPermissionService;
import com.asiainfo.cms.common.vo.LoginUser;
import com.asiainfo.cms.system.entity.SysRole;
import com.asiainfo.cms.system.entity.SysUser;
import com.asiainfo.cms.system.service.ISysUserService;
import com.asiainfo.cms.system.vo.SysDeptVO;
import com.asiainfo.cms.system.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * MyUserDetailsService 服务实现
 */
@Component
public class MyUserDetailsService implements UserDetailsService {

    @Resource
    private ISysUserService userService;

    @Autowired
    private SysPermissionService permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SysUserVO user = userService.selectUserByUserName(username);

        if (ObjectUtil.isNull(user)) {
            throw new BizException("登录用户：" + username + " 不存在");
        } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            throw new BizException("对不起，您的账号：" + username + " 已被删除");
        } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            throw new BizException("对不起，您的账号：" + username + " 已停用");
        }

        Collection<? extends GrantedAuthority> authorities = new ArrayList();
        List<SysRole> roles = user.getRoles();
        if (ObjectUtil.isNotEmpty(roles)) {
            authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
        } else {
            for (SysRole role : roles) {
                if (role.getRoleId() == 0) {
                    authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_SYSTEM");
                    break;
                } else if (role.getRoleId() == 1) {
                    authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN");
                    break;
                } else if (role.getRoleId() == 2) {
                    authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_EDIT");
                } else {
                    authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
                }
            }
        }

        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(user.getUserId());
        loginUser.setUserName(username);
        loginUser.setPassword(user.getPassword());
        loginUser.setAuthorities(authorities);
        loginUser.setPermissions(permissionService.getMenuPermission(user.getUserId()));
        return new MyUserDetails(loginUser, loginUser.getAuthorities());
    }

}

